package edu.hitsz.scorelist;

import edu.hitsz.StartGame;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

public class ScoreDaoImpl implements ScoreDao{

    public static List<Score> scores;
    private Path path;
    private String filename;

    public ScoreDaoImpl() throws IOException {
        if (StartGame.selectPattern == "edu.hitsz.application.EasyGame") {
            path = Paths.get("src/edu/hitsz/scorelist/easyscorelist.txt");
            filename = "src/edu/hitsz/scorelist/easyscorelist.txt";
        } else if (StartGame.selectPattern == "edu.hitsz.application.NormalGame") {
            path = Paths.get("src/edu/hitsz/scorelist/normalscorelist.txt");
            filename = "src/edu/hitsz/scorelist/normalscorelist.txt";
        } else {
            path = Paths.get("src/edu/hitsz/scorelist/hardscorelist.txt");
            filename = "src/edu/hitsz/scorelist/hardscorelist.txt";
        }


        scores = new ArrayList<Score>();
        File f = new File(filename);
        if (!f.exists()) {
            f.createNewFile();
        }
        List<String> lines = Files.readAllLines(path);
        for (String line : lines) {
            getUser(line);
            scores.add(getUser(line));
        }
    }

    @Override
    public void getscorelist() throws IOException{
        System.out.println("***********************************************************");
        System.out.println("                         得分排行榜                          ");
        System.out.println("***********************************************************");
        FileInputStream f = new FileInputStream(filename);
        InputStreamReader reader = new InputStreamReader(f,"UTF-8");
        StringBuffer sb = new StringBuffer();
        while (reader.ready()) {
            sb.append((char)reader.read());
        }
        System.out.println(sb.toString());
        reader.close();
        f.close();
    }

    @Override
    public void scoreadd(String name, int score) throws IOException {
        Score userscore = new Score(name,score,this.setTime());
        scores.add(userscore);
        Collections.sort(scores);
        OutputStream f = new FileOutputStream(filename);
        OutputStreamWriter writer = new OutputStreamWriter(f,"UTF-8");
        for(int i = 0;i < scores.size();i++) {
            scores.get(i).setRank(i + 1);
            writer.append("第" + scores.get(i).getRank() + "名：" + scores.get(i).getUserName() + "," + scores.get(i).getScore() + "," + scores.get(i).getTime());
            writer.append("\r\n");
        }
        writer.close();
        f.close();
    }

    @Override
    public void listclear() throws IOException {
        Files.delete(path);
    }

    @Override
    public void delete(int index) throws IOException {
        scores.remove(index);
        Collections.sort(scores);
        OutputStream f = new FileOutputStream(filename);
        OutputStreamWriter writer = new OutputStreamWriter(f,"UTF-8");
        for(int i = 0;i < scores.size();i++) {
            scores.get(i).setRank(i + 1);
            writer.append("第" + scores.get(i).getRank() + "名：" + scores.get(i).getUserName() + "," + scores.get(i).getScore() + "," + scores.get(i).getTime());
            writer.append("\r\n");
        }
        writer.close();
        f.close();
    }

    public String setTime(){
        Date time = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = format.format(time);
        return currentTime;
    }

    public Score getUser(String line){
        String name = "";
        String time;
        String st1 = "";
        int rank = Integer.parseInt(line.substring(line.indexOf('第') + 1,line.indexOf('名')));
        int i = line.indexOf("：") + 1;
        while (line.charAt(i) != ',') {
            name = name + line.charAt(i);
            i++;
        }
        i++;
        while (line.charAt(i) != ',') {
            st1 = st1 + line.charAt(i);
            i++;
        }
        i++;
        int score = Integer.parseInt(st1);
        time = line.substring(i);
        Score userScore = new Score(rank,name,score,time);
        return userScore;
    }
}
